﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ADUSWebApp.Models;

namespace ADUSWebApp.Controllers
{
    public class MoviesController : Controller
    {
        //
        // GET: /Movies/

        //public ActionResult Index()
        //{
        //    return View();
        //}

        MoviesEntities db = new MoviesEntities();

        public ActionResult Index()
        {
            var movies = from m in db.Movies
                         where m.ReleaseDate > new DateTime(1984, 6, 1)
                         select m;
            return View(movies.ToList());
        }

        //Create method
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Movie newMovie)
        {
            //用来检查提交的表单中的数据是否能够被用来创建一个Movie对象
            if (ModelState.IsValid)
            {
                db.AddToMovies(newMovie);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                return View(newMovie);
            }
        }

        public ActionResult Edit(Int32 id)
        {

            var movieToEdit = (from m in db.Movies
                               where m.Id == id
                               select m).First();
            return View(movieToEdit);
        }

        // 
        // POST: /Movies/Edit 
        [HttpPost]
        public ActionResult Edit(Movie editMovie)
        {

            var originalMovie = (from m in db.Movies
                                 where m.Id == editMovie.Id
                               select m).First();
            if (ModelState.IsValid)
            {
                db.ApplyCurrentValues<Movie>(originalMovie.EntityKey.EntitySetName, editMovie);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                return View(editMovie);
            }
        }

        // GET: /Movies/Details 
        public ActionResult Details(Int32 id)
        {
            var movie = (from m in db.Movies
                         where m.Id == id
                         select m).First();
            if (movie == null)
                return RedirectToAction("Index");
            return View("Details", movie);
        }


        //Delete method
        public ActionResult Delete(Int32 id)
        {
            var movieToDelete = (from m in db.Movies
                         where m.Id == id
                         select m).First();
            return View(movieToDelete);
        }

        [HttpPost]
        public ActionResult Delete(Movie deleteMovie)
        {

            //db.Movies movieToDelete = db.Movies.First(p => p.Id == deleteMovie.Id); 
            //var movieToDelete = from m in db.Movies
            //                     where m.Id == deleteMovie.Id
            //                     select m;
            var movieToDelete = (from m in db.Movies
                                 where m.Id == deleteMovie.Id
                                 select m).First();
            //if (ModelState.IsValid)
            //{
                db.DeleteObject(movieToDelete);
                db.SaveChanges();
                return RedirectToAction("Index");
            //}
            //else
            //{
            //    return View(deleteMovie);
            //}
         }
    }
}
